package com.example.shw

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View

const val NOTIFICATION_ID = 0x123
const val CHANNEL_ID = "shw"
class SendNotificationActivity : AppCompatActivity(){
    private var nm : NotificationManager?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sendnotification)
        nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val name = "test Channel"
        val channel = NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH)
        channel.description = "test channel's description"
        channel.enableLights(true)
        channel.enableVibration(true)
        channel.lightColor = Color.RED
        channel.vibrationPattern = longArrayOf(0, 50, 100, 150)
        nm?.createNotificationChannel(channel)
    }
    fun send(view : View){
        val intent = Intent(this@SendNotificationActivity,StackViewActivity::class.java)
        val pi = PendingIntent.getActivity(this@SendNotificationActivity, 0, intent, 0)
        val notify = Notification.Builder(this, CHANNEL_ID)
            .setAutoCancel(true)
            .setTicker("有新消息")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("一条新通知")
            .setContentIntent(pi)
            .setContentText("恭喜您，您加薪了，工资增长30%！！")
            .setWhen(System.currentTimeMillis())
            .build()
        nm?.notify(NOTIFICATION_ID, notify)
    }
    fun del (v : View){
        nm?.cancel(NOTIFICATION_ID)
    }
}